public class ThreadTest02 {
    public static void main(String[] args) {
        //实现线程的第二种方式，比第一种更常用
        MyThread02 t1 = new MyThread02();
        MyThread02 t2 = new MyThread02();

        t1.setName("t1");
        t2.setName("t2");
        t1.start();
        //System.out.println(Thread.currentThread().getName());
        t2.start();

        // t1.stop();
        try {
            Thread.sleep(1000 * 3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }//3秒后结束线程
        //t2.stop();
        t1.flag = false;
        try {
            Thread.sleep(1000 * 3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.flag = false;
    }


}
class MyThread02 extends Thread implements Runnable {
    boolean flag = true;

    public void run() {
        for (int i = 0; i < 10; i++) {
            if (flag) {
                System.out.println(Thread.currentThread().getName() + "----->" + i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    //e.printStackTrace();
                }
            } else {
                System.out.println(Thread.currentThread().getName() + "线程结束");
                return;
            }
        }
    }
}